import numpy as np
from scipy.spatial.distance import euclidean
from modules import *

# 假设我们有两个模型A和B，它们在多个评价指标上有如下表现
# 这里使用模拟数据，实际情况中应使用真实数据

# 评价指标:(带权重的)precision, recall, f1-score, MCC,params
#自己的:
MobilenetV2 = [ 0.57,0.68,0.59,-0.0139,1316354]
Mobilenet = [ 0.56 ,0.61,0.58, -0.049766,100354]

#Thesis:
mobilenet = [0.48,0.48,0.48,-0.0425258,100354]
Resnet50 = [0.51,0.51,0.49,0.018659699242974316,12845313]
Vgg16 = [0.46,0.46,0.45,-0.08536846489158217,50178]

matrix = [MobilenetV2,Mobilenet,mobilenet,Resnet50,Vgg16]

# 评价指标的权重（precision, recall, f1-score, MCC）
# 为了提高正例正确性的重要性并凸显分类性能,我们提高precision和MCC的权重
weights = [0.3, 0.2, 0.25, 0.24,0.01]

def score(model,weights):
    model = np.array(model)
    # 标准化处理
    normalized_model = model / np.sqrt((model ** 2).sum(axis=0))
    # 理想最优解和最劣解
    ideal_best = np.array([1,1,1,1,np.min(normalized_model,axis=0)[-1]])
    ideal_worst = np.array([0,0,0,-1,np.max(normalized_model,axis=0)[-1]])
    # print(ideal_best)

    # 计算每个模型与理想最优解和最劣解的相似度
    similarity_best_model = np.sqrt(((normalized_model - ideal_best) ** 2 * weights).sum(axis=1))
    similarity_worst_model =np.sqrt(((normalized_model - ideal_worst) ** 2 * weights).sum(axis=1))
    # print(similarity_best_model)

    # 计算TOPSIS得分
    score = similarity_worst_model / (similarity_best_model + similarity_worst_model)
    
    return score,normalized_model

print(score(matrix,weights))

def radar_score_fig(model,name):
    # 标准化处理
    angles = np.linspace(0,2*np.pi,len(model),endpoint = False).tolist()
    # model += model[:1]
    # angles += angles[:1]
    labels = ['Precision','Recall','F1-score','MCC','Trainable params']
    fig,ax = plt.subplots(figsize = (6,6),subplot_kw=dict(polar=True))
    ax.fill(angles,model,color = "green",alpha = 0.25)
    # ax.plot(angles,model,color = "green",linewidth = 1.5)
    ax.set_thetagrids(np.degrees(angles),labels)
    ax.set_title(f'{name} Radar-fig(partly normalized)')
    plt.show()
norm_MobilenetV2 = [ 0.57,0.68,0.59,-0.0139,0.0989]
norm_Mobilenet = [ 0.56 ,0.61,0.58, -0.049766,0.00392]
norm_mobilenet = [0.48,0.48,0.48,-0.0425258,0.00392]
norm_Resnet50 = [0.51,0.51,0.49,0.018659699242974316,1]
norm_Vgg16 = [0.46,0.46,0.45,-0.08536846489158217,0]

radar_score_fig(norm_MobilenetV2,'MobilenetV2')
radar_score_fig(norm_Mobilenet,'Mobilenet(CCMD)')
radar_score_fig(norm_mobilenet,'Mobilenet(CBIS-DDSM)')
radar_score_fig(norm_Resnet50,'Resnet50')
radar_score_fig(norm_Vgg16,'Vgg16')

def hist(x,name):
    models = ["MobilenetV2", "Mobilenet", " mobilenet", "Resnet50", "Vgg16"]
    plt.figure(figsize=(10,6))
    plt.bar(models[:2],x[:2],width = 0.3,color = 'pink')
    plt.bar(models[2:],x[2:],width = 0.3,color = 'blue')
    for i,value in enumerate(x):
        plt.text(i,value+0.01,str(value),ha='center')
    plt.xlabel('Model Type')
    plt.ylabel('Value')
    plt.title(name)
    plt.show()

# recall = [0.68,0.61,0.48,0.51,0.46]
# hist(recall,'Recall')

# Precision = [0.57,0.56,0.48,0.51,0.46]
# hist(Precision,"Precision")

# f1score = [0.59,0.58,0.48,0.49,0.45]
# hist(f1score,"F1-score")

